﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using ControllerLayer.MainView;
using System.Configuration;
using ControllerLayer.Reports;
using ModelLayer.Login;

namespace ViewLayer.MainView
{
    public partial class SearchView : Form
    {
        bool loggedUser;
        User user = User.Instance;
        public SearchView(bool isLoggedUser)
        {
            InitializeComponent();
            loggedUser = isLoggedUser;
        }

        private void patientProfile_DataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.visit_TableAdapter.Fill(this.visitDataSet.Visit_, new System.Guid(patientProfile_DataGridView[1, e.RowIndex].Value.ToString()));
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private void visit_DataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                string accessionNumber = visit_DataGridView[1, e.RowIndex].Value.ToString();
                string pid = patientProfile_DataGridView[7, patientProfile_DataGridView.SelectedRows[0].Index].Value.ToString();
                try
                {
                    Process.Start(ConfigurationManager.AppSettings["ExecPath"].ToString(), accessionNumber + " " + pid);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(null, "No se puede abrir la aplicación.\nDebe configurar la ruta de acceso del programa en el archivo de configuración.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                MainViewController.loadFlag = false;
                MainViewController.userLogOutFlag = false;
                this.Close();
            }
        }

        private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (visit_DataGridView.Rows.Count > 0)
                    this.visit_TableAdapter.Fill(this.visitDataSet.Visit_, new System.Guid("00000000000000000000000000000000"));

                if (!(textBoxName.Text == "" && textBoxLastName.Text == "" && textBoxPID.Text == ""))
                {
                    if (comboBoxAssignedPractitioner.SelectedItem.ToString() == "" && comboBoxModalities.SelectedItem.ToString() == "" && !checkBoxEnableDate.Checked)
                    {
                        if (textBoxName.Text == "")
                            textBoxName.Text = null;
                        if (textBoxLastName.Text == "")
                            textBoxLastName.Text = null;
                        if (textBoxPID.Text == "")
                            textBoxPID.Text = null;
                        try
                        {
                            //this.patientProfile_TableAdapter.FillBySearch(this.patientInformationDataSet.PatientProfile_, textBoxLastName.Text + "%", textBoxName.Text + "%", textBoxPID.Text + "%");
                            patientProfile_DataGridView.DataSource = ReportsController.GetPatientsTable(textBoxLastName.Text, textBoxName.Text, textBoxPID.Text, "", "");
                        }
                        catch (System.Exception ex)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.Message);
                        }
                    }
                    else
                    {
                        try
                        {
                            if (checkBoxEnableDate.Checked)
                                patientProfile_DataGridView.DataSource = ReportsController.GetPatientsTableWithDate(textBoxLastName.Text, textBoxName.Text, textBoxPID.Text, comboBoxAssignedPractitioner.SelectedItem.ToString(), comboBoxModalities.SelectedItem.ToString(), dateTimePickerStudyDate.Text);
                            else
                                patientProfile_DataGridView.DataSource = ReportsController.GetPatientsTable(textBoxLastName.Text, textBoxName.Text, textBoxPID.Text, comboBoxAssignedPractitioner.SelectedItem.ToString(), comboBoxModalities.SelectedItem.ToString());
                        }
                        catch (System.Exception ex)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.Message);
                        }
                    }
                }
                else
                {
                    if (comboBoxAssignedPractitioner.SelectedItem.ToString() == "" && comboBoxModalities.SelectedItem.ToString() == "" && !checkBoxEnableDate.Checked)
                    {
                        //this.patientProfile_TableAdapter.FillBySearch(this.patientInformationDataSet.PatientProfile_, "", "", "-1");
                        patientProfile_DataGridView.DataSource = ReportsController.GetPatientsTable(textBoxLastName.Text, textBoxName.Text, textBoxPID.Text, "", "");
                    }
                    else
                    {
                        try
                        {
                            if (checkBoxEnableDate.Checked)
                                patientProfile_DataGridView.DataSource = ReportsController.GetPatientsTableWithDate(textBoxLastName.Text, textBoxName.Text, textBoxPID.Text, comboBoxAssignedPractitioner.SelectedItem.ToString(), comboBoxModalities.SelectedItem.ToString(), dateTimePickerStudyDate.Text);
                            else
                                patientProfile_DataGridView.DataSource = ReportsController.GetPatientsTable(textBoxLastName.Text, textBoxName.Text, textBoxPID.Text, comboBoxAssignedPractitioner.SelectedItem.ToString(), comboBoxModalities.SelectedItem.ToString());
                        }
                        catch (System.Exception ex)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.Message);
                        }
                    }
                }
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            this.TextBox_KeyPress(null, new KeyPressEventArgs((char)13));
        }

        private void SearchView_Load(object sender, EventArgs e)
        {
            DataTable radiologists = ReportsController.GetRadiologists();
            DataTable modalities = ReportsController.GetModalities();
            List<string> assignedPracList = new List<string>();
            List<string> modalitiesList = new List<string>();
            for (int i = 0; i < radiologists.Rows.Count; i++)
            {
                assignedPracList.Add(radiologists.Rows[i].ItemArray[1].ToString());
                //this.comboBoxAssignedPractitioner.Items.Add(radiologists.Rows[i].ItemArray[1].ToString());
            }
            assignedPracList.Add("");
            assignedPracList.Sort();
            comboBoxAssignedPractitioner.DataSource = assignedPracList;
            

            for (int i = 0; i < modalities.Rows.Count; i++)
            {
                modalitiesList.Add(modalities.Rows[i].ItemArray[0].ToString());
                //this.comboBoxModalities.Items.Add(modalities.Rows[i].ItemArray[0].ToString());
            }
            
            modalitiesList.Add("");
            modalitiesList.Sort();
            comboBoxModalities.DataSource = modalitiesList;
            comboBoxModalities.SelectedIndex = 0;
            if (loggedUser)
            {
                dateTimePickerStudyDate.Enabled = true;
                checkBoxEnableDate.Checked = true;
                comboBoxAssignedPractitioner.SelectedItem = user.getName();
                this.TextBox_KeyPress(null, new KeyPressEventArgs((char)13));
            }
            else
            {
                dateTimePickerStudyDate.Enabled = false;
                checkBoxEnableDate.Checked = false;
                comboBoxAssignedPractitioner.SelectedIndex = 0;
            }
        }

        private void checkBoxEnableDate_CheckedChanged(object sender, EventArgs e)
        {
            dateTimePickerStudyDate.Enabled = checkBoxEnableDate.Checked;
        }
    }
}
